#include <QtGui>

#include "bunker.h"

/* Bunker is made of 10 tiles arranged like so
 *
 *   SE SQ SQ SW
 *   SQ NW NE SQ
 *   SQ       SQ
 */

Bunker::Bunker (const QString &squareGif,
                const QString &neCornerGif,
                const QString &seCornerGif,
                const QString &swCornerGif,
                const QString &nwCornerGif,
                QWidget *parent)
  : QFrame (parent)
{
  int x = 0;
  int y = 0;

  QLabel *tile = createTile (seCornerGif);
  QSize size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x += size.width ();

  tile = createTile (squareGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x += size.width ();

  tile = createTile (squareGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x += size.width ();

  tile = createTile (swCornerGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x = 0;
  y += size.height ();

  tile = createTile (squareGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x += size.width ();

  tile = createTile (nwCornerGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x += size.width ();

  tile = createTile (neCornerGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x += size.width ();

  tile = createTile (squareGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x = 0;
  y += size.height ();

  tile = createTile (squareGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
  x += 3 * size.width ();

  tile = createTile (squareGif);
  size = tile->sizeHint ();
  tile->setGeometry (x, y, size.width (), size.height ());
  tile->setMouseTracking (true);
  _tiles.insert (std::pair<int, QLabel *> (x, tile));
}

bool Bunker::tryHit (const QRect &rect)
{
  std::multimap<int, QLabel *>::iterator beg = _tiles.lower_bound (
    rect.x () - 10);
  std::multimap<int, QLabel *>::iterator end = _tiles.upper_bound (
    rect.x () + 10);

  for (; beg != end; ++beg) {
    if (beg->second->isHidden ())
      continue;

    if (beg->second->geometry ().intersects (rect)) {
      QMovie *movie = beg->second->movie ();
      movie->jumpToNextFrame ();

      if (movie->currentFrameNumber () == 0)
        beg->second->setVisible (false);

      return true;
    }
  }

  return false;
}

void Bunker::reset ()
{
  std::multimap<int, QLabel *>::iterator beg = _tiles.begin ();
  std::multimap<int, QLabel *>::iterator end = _tiles.end ();

  for (; beg != end; ++beg) {
    beg->second->movie ()->jumpToFrame (0);
    beg->second->setVisible (true);
  }
}

QSize Bunker::sizeHint () const
{
  std::multimap<int, QLabel *>::const_iterator beg = _tiles.begin ();

  return QSize (4 * beg->second->width (), 3 * beg->second->height ());
}

QLabel * Bunker::createTile (const QString &file)
{
  QMovie *m = new QMovie (this);
  m->setFileName (file);
  m->setCacheMode (QMovie::CacheAll);
  m->jumpToFrame (0);

  QLabel *ret = new QLabel (this);
  ret->setMovie (m);

  return ret;
}
